package com.witmed.mymdm.mvi

import androidx.lifecycle.viewModelScope
import com.witmed.lib_base.BaseViewModel
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flatMapConcat
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch

class SlashViewModel:BaseViewModel<SlashResp,WhiteState>() {
    override fun createState(): WhiteState {
        return WhiteState(false,false,ArrayList())
    }

    override fun createResp(): SlashResp {
        return SlashResp()
    }

    fun selWhite(){
        viewModelScope.launch {
            ComFlow(resp.selLock()).flatMapConcat {
                if(it.statusCode==200){
                    val errorEntity = LockErrorEntity()
                    errorEntity.statusCode=100
                    errorEntity.msg=it.msg
                    return@flatMapConcat ComFlow(flow {emit(errorEntity)})
                }else{
                    return@flatMapConcat ComFlow(resp.selWhite())
                }
            }.collect{
                if(it.statusCode==100){
                    mutableContainer.updateUiState { copy(true,false,ArrayList()) }

                }else{
                    val entity = it as WhiteEntity
                    mutableContainer.updateUiState { copy(false,true,entity.datas) }
                }
            }
        }
    }
}